package logic

import (
	"context"
	"fmt"
	"jd/model/mysql"
	"jd/model/redis"
	"strconv"

	"jd/userrpc/internal/svc"
	"jd/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpdateLogic) UserUpdate(in *userrpc.UserUpdateRequest) (*userrpc.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line

	get := redis.Rdb.Get("sendSmslogin" + strconv.Itoa(int(in.UserId)))

	if get.Val() == "" {
		return nil, fmt.Errorf("暂未获取短信验证码")
	}
	if get.Val() != in.SendSmsCode {
		return nil, fmt.Errorf("短信验证码输入错误")
	}

	userAdd := mysql.Useradd{
		Image:    in.Image,
		Account:  in.Account,
		Name:     in.Name,
		Sex:      in.Sex,
		BirthDay: in.Birthday,
		UserId:   int(in.UserId),
	}
	err := mysql.DB.Model(&mysql.Useradd{}).Where("user_id=?", in.UserId).Updates(&userAdd).Error
	if err != nil {
		return nil, fmt.Errorf("修改失败")
	}
	return &userrpc.UserUpdateResponse{}, nil
}
